<template>
  <!--项目备案start -->
  <el-dialog
    v-dialogDrag
    :title="dialogTitle"
    :close-on-click-modal="true"
    :visible.sync="dialogVisible"
    class="dialogStyleMiddle"
    :before-close="cancel"
  >
    <el-form
      ref="addForm"
      class="dialog-form"
      :model="formData"
      label-width="80px"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="" prop="recordTime">
            <template slot="label">
              <span class="labelTitle">备案日期</span>
            </template>
            <el-date-picker
              v-model="formData.recordTime"
              readonly
              style="width:100%"
              size="medium"
              type="daterange"
              value-format="yyyy-MM-dd"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              clearable
              @change="recordTimeChange"
            />

          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备案地区" prop="regaionList">
            <!--            <el-cascader-->
            <!--              v-model="formData.regaionList"-->
            <!--              size="medium"-->
            <!--              placeholder="请选择所在地区"-->
            <!--              :options="cityOptions"-->
            <!--              clearable-->
            <!--              style="width: 100%"-->
            <!--              @change="handleRegion"-->
            <!--            >-->
            <!--              <template slot-scope="{ data }">-->
            <!--                <span>{{ data.label }}</span>-->
            <!--              </template>-->
            <!--            </el-cascader>-->
            <el-input v-model="currentProvince" readonly clearable size="medium" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="详细地址" prop="address">
            <el-input v-model="formData.address" readonly clearable size="medium" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备案说明" prop="recordExplain">
            <el-input v-model="formData.recordExplain" readonly clearable type="textarea" :rows="3" size="medium" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备案证明" prop="appFiles">
            <FileUplode
              ref="fileUplode"
              :edit-file-list="filingList"
              :read-only="isReadonly"
              @getFileList="getFiling"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
  <!--项目备案end -->
</template>

<script>
// 页面接口
import { listEnterpriseRecordDetail } from '@/api/system/EnterpriseFiling'
// 上传文件
import FileUplode from '@/components/Upload/FileUplode'
export default {
  components: {
    FileUplode
  },
  props: {
    cityOptions: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      /* 新增 start*/
      isReadonly: true,
      dialogTitle: '', // 弹层标题
      dialogVisible: false, // 弹层是否显示
      currentProvince: '',
      formData: { // 新增数据
        id: '', // 项目id
        recordType: '0',
        recordTime: '', // 备案时间
        recordStartDate: '',
        recordEndDate: '',
        regaionList: '',
        address: '',
        recordExplain: '',
        appFiles: ''
      },
      status: '2',
      /* 新增 end*/
      readOnly: true,
      filingList: []// 附件
    }
  },
  methods: {
    // 备案日期变化
    recordTimeChange(e) {
      this.formData.recordStartDate = e[0]
      this.formData.recordEndDate = e[1]
    },
    handleRegion(regionList) {
      const [nation, region, province, city, county] = regionList
      this.formData = { ...this.formData, nation, region, province, city, county }
    },
    /* 新增/编辑弹层 start*/
    // 新增/编辑
    handleAdd(title, id) {
      this.dialogTitle = title
      this.dialogVisible = true
      if (id) {
        this.getDetailById(id)
      }
    },
    // 查询备案的详情
    getDetailById(id) {
      listEnterpriseRecordDetail(id).then(res => {
        if (res.data) {
          const { nation, region, province, city, county, appFiles } = res.data
          this.formData = { ...this.formData, ...res.data }
          this.formData.regaionList = [nation, region, province, city, county]
          if (res.data.recordStartDate && res.data.recordEndDate) this.formData.recordTime = [this.formData.recordStartDate, this.formData.recordEndDate]
          this.filingList = appFiles
          this.getRegionCode(province, city, county)
        }
      })
    },
    // 根据地区编码回显地区
    getRegionCode(province, city, county) {
      if (!county) return
      const nation = this.cityOptions[0]
      const region = this.cityOptions[0].children.find(item => {
        return item.children && item.children.some(city => city.value === province)
      })
      const sheng = region.children.find(sheng => sheng.value === province)
      const shi = sheng.children.find(shi => shi.value === city)
      const xian = shi.children.find(xian => xian.value === county)
      this.currentProvince = (nation.label + region.label + sheng.label + shi.label + xian.label) || ''
    },
    // 取消按钮
    cancel() {
      this.dialogVisible = false
    },
    /* 新增弹层 end*/
    /** ************ 文件上传start *******************/
    getFiling(fileList, deleteFileIds) {
      this.formData.appFiles = fileList
    }
    /** ************ 文件上传end *******************/
  }

}
</script>

<style>

</style>
